package com.wyz.saas.common.utils

import android.location.Location
import com.vividsolutions.jts.geom.Point

object AndroidLocationUtils {
    fun locationToPoint(location: Location, z: Double = 0.0): Point {
        return GeometryUtils.createPoint(
            location.longitude,
            location.latitude,
            (z <= 0.0).yes { location.altitude }.otherwise { z }
        )
    }

    fun locationToJson(location: Location): String {
        return JacksonUtils.objectMapper().writeValueAsString(locationToPoint(location))
    }

    fun jsonToLocation(json: String, provider: String = "CUSTOM"): Location {
        val jsonToPoint = jsonToPoint(json)
        return Location(provider).apply {
            this.longitude = jsonToPoint.coordinate.x
            this.latitude = jsonToPoint.coordinate.y
            this.altitude = jsonToPoint.coordinate.z
        }
    }

    fun jsonToPoint(json: String): Point {
        return JacksonUtils.objectMapper().readValue(json, Point::class.java)
    }
}